<%@ page import="com.nextStep.entity.Users" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="com.nextStep.entity.DbConfig" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="java.lang.reflect.Method" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>mySqlExecute【数据库管理】</title>

    <!-- GOOGLE FONTS -->
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,500|Poppins:400,500,600,700|Roboto:400,500" rel="stylesheet"/>
    <link href="https://cdn.materialdesignicons.com/3.0.39/css/materialdesignicons.min.css" rel="stylesheet" />

    <!-- PLUGINS CSS STYLE -->
    <link href="../plugins/toaster/toastr.min.css" rel="stylesheet" />
    <link href="../plugins/nprogress/nprogress.css" rel="stylesheet" />
    <link href="../plugins/flag-icons/css/flag-icon.min.css" rel="stylesheet"/>
    <link href="../plugins/jvectormap/jquery-jvectormap-2.0.3.css" rel="stylesheet" />
    <link href="../plugins/ladda/ladda.min.css" rel="stylesheet" />
    <link href="../plugins/select2/css/select2.min.css" rel="stylesheet" />
    <link href="../plugins/daterangepicker/daterangepicker.css" rel="stylesheet" />

    <!-- SLEEK CSS -->
    <link id="sleek-css" rel="stylesheet" href="../css/sleek.css" />



    <!-- FAVICON -->
    <link href="../img/favicon.png" rel="shortcut icon" />

    <!--
      HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries
    -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../plugins/nprogress/nprogress.js"></script>
</head>

<%--class="sidebar-fixed sidebar-dark header-light header-fixed"--%>
<body  id="body">
<%--<script>
    NProgress.configure({ showSpinner: false });
    NProgress.start();
</script>--%>

<%--判断是否登录--%>
<%
    if(session.getAttribute("LOGIN_DATA")==null){
        response.sendRedirect("../login.jsp");
        return ;
    }

    Map map = (Map) session.getAttribute("DATABASE_DATA");   //当前用户的数据库名(map-key)--》表名（list-->map-value)
    List<DbConfig> userDbConfig = (List<DbConfig>) session.getAttribute("userDbConfig");   //当前用户的所有数据库配置

%>


<div class="mobile-sticky-body-overlay"></div>

<div class="wrapper">

    <div class="page-wrapper">
        <!-- Header -->
        <header class="main-header " id="header">
            <nav class="navbar navbar-static-top navbar-expand-lg">
                <!-- Sidebar toggle button -->
                <%--<button id="sidebar-toggler" class="sidebar-toggle">--%>
                <span class="sr-only">Toggle navigation</span>
                <%--</button>--%>
                <!-- search form -->
                <div class="search-form d-none d-lg-inline-block">
                    <div class="input-group">
                        <button type="button" name="search" id="search-btn" class="btn btn-flat">
                            <i class="mdi mdi-magnify"></i>
                        </button>
                        <input type="text" name="query" id="search-input" class="form-control" value="mysqlExecute 数据库配置管理"
                               autofocus autocomplete="off"  readonly="readonly" style="font-size: 28px"/>
                    </div>
                    <div id="search-results-container">
                        <ul id="search-results"></ul>
                    </div>
                </div>

                <div class="navbar-right ">
                    <ul class="nav navbar-nav">
                        <!-- 菜单栏 -->
                        <li class="dropdown user-menu">
                            <button href="#" class="dropdown-toggle nav-link" data-toggle="dropdown">
                                <img src="../img/user/user.png" class="user-image" alt="User Image" />
                                <span class="d-none d-lg-inline-block"><%=((Users)session.getAttribute("LOGIN_DATA")).getName() %></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <!-- User image -->
                                <li class="dropdown-header">
                                    <img src="../img/user/user.png" class="img-circle" alt="User Image" />
                                    <div class="d-inline-block">
                                        <%=((Users)session.getAttribute("LOGIN_DATA")).getName()%> <small class="pt-1"><%=((Users)session.getAttribute("LOGIN_DATA")).getEmail()%></small>
                                    </div>
                                </li>
                                <li>
                                    <a href="index"><i class="mdi mdi-account"></i> 主页</a>
                                </li>
                                <li>
                                    <a href="dbConfigAdmin"> <i class="mdi mdi-settings"></i> 数据库配置 </a>
                                </li>
                                <li>
                                    <a href="sqlplus"> <i class="mdi mdi-settings"></i> sqlPlus 命令操作</a>
                                </li>
                                <li class="dropdown-footer">
                                    <a href="../user/exit"> <i class="mdi mdi-logout"></i> 退出 </a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>

            </nav>


        </header>

        <div class="content-wrapper">

                        <div class="card-header justify-content-between align-items-center card-header-border-bottom">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <font size="5" >数据库配置管理</font>&nbsp;&nbsp;&nbsp;&nbsp;
                            <button class="btn btn-primary btn-pill"  onclick="addData()">添加配置 </button> &nbsp;&nbsp;&nbsp;&nbsp;
                            <button class="btn btn-primary btn-pill"  onclick="index()">主页 </button>

                        </div>
                        <div class="content">
                            <h4  class="d-inline-block"> [ <a  >数据库配置</a> ].表 </h4>
                        <div style="overflow-x:scroll;">
                            <table id="table" class="table table-bordered">

                                <%

                                    for (int i = 0; i < userDbConfig.size() ; i++) {
                                        DbConfig dbConfig = userDbConfig.get(i);
                                %>
                                        <tr>
                                <%
                                    String rowId="";
                                    Field[] field = dbConfig.getClass().getDeclaredFields();  //获取实体类的所有属性，返回Field数组
                                        for (int j = 1; j < field.length; j++) {  //遍历所有属性
                                            String name = field[j].getName();    //获取属性的名字
                                            name = name.substring(0, 1).toUpperCase() + name.substring(1); //将属性的首字符大写，方便构造get，set方法
                                            Method m = dbConfig.getClass().getMethod("get" + name);
                                            String value= (String) m.invoke(dbConfig);    //调用getter方法获取属性值

                                %>
                                                <td><%=value%></td>
                                <%
                                            if(j == field.length-1){   //保存本条数据的id
                                                rowId = value;
                                            }
                                        }
                                %>
                                            <%
                                                if(i==0){
                                            %>
                                            <td><a >操作</a> </td>
                                            <%
                                                }else {
                                            %>
                                            <td>
                                                <button onclick="updateData( '<%=rowId%>')"><font color='#4c84ff'>修改配置</font></button>&nbsp;&nbsp;&nbsp;&nbsp;
                                                <button onclick="deleteData('<%=rowId%>')"><font color='#4c84ff'>删除配置</font></button>
                                            </td>

                                            <%
                                                }
                                            %>

                                        </tr>
                                <%

                                    }
                                %>
                            </table>


            </div>



        </div>

            <jsp:include page="footer.jsp"/>

    </div>


</div>


<script src="../plugins/jquery/jquery.min.js"></script>
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../plugins/toaster/toastr.min.js"></script>
<script src="../plugins/slimscrollbar/jquery.slimscroll.min.js"></script>
<script src="../plugins/charts/Chart.min.js"></script>
<script src="../plugins/ladda/spin.min.js"></script>
<script src="../plugins/ladda/ladda.min.js"></script>
<script src="../plugins/jquery-mask-input/jquery.mask.min.js"></script>
<script src="../plugins/select2/js/select2.min.js"></script>
<script src="../plugins/jvectormap/jquery-jvectormap-2.0.3.min.js"></script>
<script src="../plugins/jvectormap/jquery-jvectormap-world-mill.js"></script>
<script src="../plugins/daterangepicker/moment.min.js"></script>
<script src="../plugins/daterangepicker/daterangepicker.js"></script>
<script src="../plugins/jekyll-search.min.js"></script>
<script src="../js/sleek.js"></script>
<script src="../js/chart.js"></script>
<script src="../js/date-range.js"></script>
<script src="../js/map.js"></script>
<script src="../js/custom.js"></script>

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script type="text/javascript">


    function index() {
        window.location.replace("index");
    }
    function updateData(dbid) {
       // alert("updateData:"+rowId)
        window.location.href="upDbConfigPage?dbid="+dbid;
    }

    function deleteData(rowId){
        //alert("deleteData:"+rowId)
        $.ajax({
            url: "dbconfigDel",
            type: "post",
            data: {dbid:rowId},
            success: function (status) {
                if(status == 1){
                    window.location.reload();
                }else {
                    alert("当前表结构可能不支持nextStep的操作，请使用sql命令操作");

                }
            }

        })
    }

    function addData() {
        window.location.href="addDbConfigPage";
    }

/*

    //查询总页数
    document.getElementById("submit").onclick=function(){
        var dbName = document.getElementById("dbName").innerHTML;  //数据库下拉框对象
        var tbName = document.getElementById("tbName").innerHTML;
        var rowId = document.getElementsByName("rowId")//获取所有name属性值等于‘tbValue’的input
        var rowValue = document.getElementsByName("rowValue")//获取所有name属性值等于‘tbValue’的input
        var arrId=new Array();
        var arrValue = [];
        for(var i=0; i<rowId.length;i++){
            arrId[i] = ( rowId[i].innerText);
           // alert ( rowId[i].innerText);  获取a标签value值
            arrValue[i] = ( rowValue[i].value);
        }

       $.ajax({
            url: "addRow",
            type: "post",
            traditional : true,   //传输数组
            data: {dbName: dbName, tbName: tbName,arrId:arrId,arrValue:arrValue},
            success: function (orderColumns) {
                if(orderColumns == 0 ){
                    alert("表结构原因，无法自动生成sql（主键原因），建议使用sql直接插入")
                }
                localStorage.tbName="";
                window.location.href="index";
            }
        });


    }
*/




</script>




</body>
</html>
